package com.app.mymusic.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.app.mymusic.R;
import com.app.mymusic.ui.BasicActivity;
import com.app.mymusic.ui.MainActivity;
import com.app.mymusic.utils.ToastUtil;
import com.app.mymusic.view.CheckView;
import com.app.mymusic.view.CircleBar;

import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends BasicActivity {

    @BindView(R.id.loginName)
    EditText loginName;
    @BindView(R.id.loginPsw)
    EditText loginPsw;
    @BindView(R.id.login_btn)
    AppCompatButton loginBtn;
    @BindView(R.id.checkview)
    CheckView checkView;

    @Inject
    LoginPresenter presenter;
  /*  @BindView(R.id.login_bar)
    NumberProgressBar loginBar;*/

    Timer timer;
    @BindView(R.id.circle_bar)
    CircleBar circleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        DaggerLoginComponent.builder()
                .loginModule(new LoginModule(LoginActivity.this))
                .build()
                .inject(this);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

    }

    int i=0;
    boolean b=true;
    private void updateBar() {
        timer=new Timer();
        if(i==100){
            b=false;
        }
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            //更新UI

                            if(i>100){
                                i=0;
                            }
                            circleBar.start(i);
                            i++;
                        }
                    });
                }
            },100,100);

    }

    @OnClick(R.id.login_btn)
    public void onClick() {
/*        checkView.unCheck();
//        circleBar.start(50);
//        updateBar();*/
        String name = loginName.getText().toString().trim();
        String psw = loginPsw.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            loginName.setError(getString(R.string.inputName_error));
            return;
        }
        if (TextUtils.isEmpty(psw)) {

            loginPsw.setError(getString(R.string.inputPsw_error));
            return;
        }
        presenter.Login(name, psw, "1212121");

    }

    public void showSucess(String res) {
        if (TextUtils.isEmpty(res)) {
            ToastUtil.show(this);
        } else {
            if (TextUtils.equals(res.split(",")[1], "true")) {
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
            } else {
                ToastUtil.showShort(LoginActivity.this, R.string.login_error);
            }
        }
    }

    public void showError(String res) {
        Toast.makeText(this, R.string.inputName_error, Toast.LENGTH_SHORT).show();
    }
}
